package com.zqq.base

import android.widget.EditText
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter

/**
 * 用于绑定view的text的属性的的值和livedata  双向绑定的时候的数据类型转换  看 https://blog.csdn.net/weixin_41648633/article/details/149909822?spm=1001.2014.3001.5501
 */
class BinddingAdapter {
    companion object{
        @BindingAdapter("android:text")
        @JvmStatic
        fun setIntValue(view: EditText, value:Number?){
            if(view.text.toString() != value?.toString()){
                view.setText(value?.toString()?:"")
            }
        }

        @InverseBindingAdapter(attribute = "android:text")
        @JvmStatic
        fun getIntValue(view: EditText):Int{
            return try{view.text.toString().toInt()}catch (e:Exception){0}
        }
    }
}